class EasingOff extends egret.DisplayObjectContainer {
    private ball:Ball
    private easing:number = 0.1
    private targetX:number = 0
    private targetY:number = 0

    public constructor() {
        super()
        this.once( egret.Event.ADDED_TO_STAGE, this.onAddToStage, this)
    }

    private onAddToStage(event:egret.Event) {
        this.targetX = this.stage.stageWidth / 2
        this.targetY = this.stage.stageHeight / 2
        this.ball = new Ball()
        this.addChild(this.ball)
        this.ball.x = 0
        this.ball.y = 0
        this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this)
    }

    private onEnterFrame(event:egret.Event) {
        let dx = this.targetX - this.ball.x
        let dy = this.targetY - this.ball.y
        let pp = egret.Point.create(dx, dy)
        try {
            let distance = pp.length
            if (distance < 0.25) {
                this.ball.x = this.targetX
                this.ball.y = this.targetY
                this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this)
                return
            }
        } finally {
            egret.Point.release(pp)
        }
        this.ball.x += (this.targetX - this.ball.x) * this.easing
        this.ball.y += (this.targetY - this.ball.y) * this.easing
    }
}